/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package coba.service.impl;

import java.util.Calendar;
import java.util.GregorianCalendar;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;

import coba.model.NumeracionSolicitud;
import coba.service.NumeracionSolicitudLocalServiceUtil;
import coba.service.base.NumeracionSolicitudLocalServiceBaseImpl;

/**
 * The implementation of the NumeracionSolicitud local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link coba.service.NumeracionSolicitudLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author rodri042
 * @see coba.service.base.NumeracionSolicitudLocalServiceBaseImpl
 * @see coba.service.NumeracionSolicitudLocalServiceUtil
 */
public class NumeracionSolicitudLocalServiceImpl
	extends NumeracionSolicitudLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link coba.service.NumeracionSolicitudLocalServiceUtil} to access the NumeracionSolicitud local service.
	 */

	public String proximoNumero() throws SystemException, PortalException {
		synchronized (NumeracionSolicitudLocalServiceImpl.class) {
			Calendar cal = GregorianCalendar.getInstance();

			long anio = cal.get(Calendar.YEAR);

			NumeracionSolicitud numeracion = this.fetchNumeracionSolicitud(1L);
		
			if (numeracion == null) {
				numeracion = NumeracionSolicitudLocalServiceUtil.createNumeracionSolicitud(1L);
				numeracion.setAnio(anio);
				numeracion.setProximoNumero(2L);
				NumeracionSolicitudLocalServiceUtil.addNumeracionSolicitud(numeracion);
			
				return "1/" + anio % 100;
			}
			else {
				if (numeracion.getAnio() == anio) {
					long proximoNumero = numeracion.getProximoNumero();
				
					numeracion.setProximoNumero(proximoNumero + 1);
					NumeracionSolicitudLocalServiceUtil.updateNumeracionSolicitud(numeracion);

					return proximoNumero + "/" + anio % 100;
				}
				else {
					numeracion.setAnio(anio);
					numeracion.setProximoNumero(2L);
					NumeracionSolicitudLocalServiceUtil.updateNumeracionSolicitud(numeracion);
				
					return "1/" + anio % 100;
				}
			}
		}

	}

}
